//
//  ViewController.m
//  什么
//
//  Created by 陈金山 on 16/1/4.
//  Copyright © 2016年 陈金山. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
{
    CALayer *znzLayer;
}

@property(nonatomic,strong)CLLocationManager *locationManger;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if ([CLLocationManager headingAvailable]) {
        znzLayer = [[CALayer alloc] init];
        NSInteger screenHeight = [UIScreen mainScreen].bounds.size.height;
        NSInteger y = (screenHeight - 320) / 2;
        znzLayer.frame = CGRectMake(0, y, 320, 320);
        znzLayer.backgroundColor = (__bridge CGColorRef _Nullable)(([UIColor blueColor]));
        [self.view.layer addSublayer:znzLayer];
        
        self.locationManger = [[CLLocationManager alloc] init];
        self.locationManger.delegate = self;
        [self.locationManger startUpdatingHeading];
    }
    
}

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
    
    CATransform3D fromValue = znzLayer.transform;
                                                      
    CGFloat headings = -1.0f * M_PI * newHeading.magneticHeading/180.0f;
    
    anim.fromValue = [NSValue valueWithCATransform3D:fromValue];
    
    CATransform3D toValue = CATransform3DMakeRotation(headings, 0, 0, 1);
    
    
    anim.toValue = [NSValue valueWithCATransform3D:toValue];
    
    anim.duration = 0.5;
    anim.removedOnCompletion = YES;
    
    znzLayer.transform = toValue;
    
    [znzLayer addAnimation:anim forKey:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
